<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="dialogVisible = true"
          >添加分类</el-button
        >
      </div>
      <el-table :data="list" style="width: 100%">
        <el-table-column type="index" label="序号" width="180">
        </el-table-column>
        <el-table-column prop="cate_alias" label="分类名称" width="180">
        </el-table-column>
        <el-table-column prop="cate_name" label="分类别名"> </el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button
              type="primary"
              size="mini"
              @click="getArtCateById(row.id)"
              >修改</el-button
            >
            <el-button type="danger" size="mini" @click="delArtCate(row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 点击添加,出现的添加填框 -->
    <el-dialog
      title="添加文章分类"
      :visible.sync="dialogVisible"
      width="40%"
      @close="resetForm"
    >
      <!-- 添加内容的地方 -->
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="ruleForm.cate_name"></el-input>
        </el-form-item>

        <el-form-item label="分类别类" prop="cate_alias">
          <el-input v-model="ruleForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addArtCate">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改区域的内容 -->
    <el-dialog title="修改文章分类" :visible.sync="uDialogVisible" width="40%">
      <el-form
        :model="uRuleForm"
        :rules="rules"
        ref="uRuleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="uRuleForm.cate_name"></el-input>
        </el-form-item>

        <el-form-item label="分类别类" prop="cate_alias">
          <el-input v-model="uRuleForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="uDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUArtCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "ArtCate",
  data() {
    return {
      ruleForm: {
        cate_name: "",
        cate_alias: "",
      },
      uRuleForm: {
        cate_name: "",
        cate_alias: "",
      },
      rules: {
        cate_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
          {
            pattern: /^\S{1,10}$/,
            message: "请输入长度为1-10的非空内容",
            trigger: "blur",
          },
        ],
        cate_alias: [
          { required: true, message: "请输入分类别名", trigger: "blur" },
          {
            pattern: /^[A-Za-z0-9]{1,15}$/,
            message: "请输入长度为1-15的字母或者数字",
            trigger: "blur",
          },
        ],
      },
      list: [],
      dialogVisible: false,
      uDialogVisible: false,
    };
  },
  created() {
    this.getArtCateList();
  },
  methods: {
    // 根据id获取文章的分类数据. 获取修改表单
    async getArtCateById(id) {
      const { data: res } = await this.$http.get("/my/cate/info", {
        params: { id },
      });
      if (res.code != 0) return this.$message.error(res.message);
      this.$message.success(res.message);
      this.uRuleForm = res.data;
      this.uDialogVisible = true;
    },
    // 确认修改的内容,确认上传
    addUArtCate() {
      this.$refs.uRuleForm.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await this.$http.put(
          "my/cate/info",
          this.uRuleForm
        );
        console.log(res);
        if (res.code != 0) return this.$message.error(res.message);
        this.$message.success(res.message);
        this.getArtCateList();
        this.uDialogVisible = false;
      });
    },
    // 删除文章分类
    delArtCate(id) {
      this.$confirm("此操作将永久删除该内容, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const { data: res } = await this.$http.delete("my/cate/del", {
            params: { id },
          });
          if (res.code != 0) return this.$message.error(res.message);
          this.$message.success(res.message);
          this.getArtCateList();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //  1.绑定事件
    // 2.兜底效验
    // 3.发送ajax请求
    // 4.添加成功,重新渲染数据
    // 5.关闭弹窗
    // 添加内容
    addArtCate() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await this.$http.post(
          "my/cate/add",
          this.ruleForm
        );
        // console.log(res);
        if (res.code != 0) return this.$message.error(res.message);
        this.$message.success(res.message);
        this.getArtCateList();
        // this.resetForm();
        this.dialogVisible = false;
      });
    },
    // 重置表单
    resetForm() {
      this.$refs.ruleForm.resetFields();
    },
    // 获取表单内容,用户信息
    async getArtCateList() {
      const { data: res } = await this.$http.get("my/cate/list");
      // console.log(res);
      this.list = res.data;
    },
  },
};
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>